from bag_of_words import BagOfWords
from dataset_builder import DatasetBuilder
from our_agent_comparator import OurAgentComparator
from nearest_neighbor import NearestNeighbor
from agent_gonzales import Gonzales
from time import clock

comparator = OurAgentComparator()
learnTasks = comparator.getLearningTasks()

stemming, stopWords = False, True
minFeatures, maxFeatures, step = 5,100, 10

classifyTime = 2
learnMinutes = 10



for name, lTask in learnTasks.items():
    for nFeatures in xrange(minFeatures,maxFeatures, step):
        startTime = clock()
        
        stopwordsFile = open('results/im_stopwords.txt','a')
        regularFile  = open('results/im_regular_stopwords.txt','a')
        stopwordsVsRegularFile = open('results/im_stopwordsVsRegular.txt','a')
        
        agentStopwords = Gonzales(stemming, stopWords, nFeatures)
        agentNothing = Gonzales(False, False, nFeatures)
        confusionStopwords, confusionNothing, mcnemar = comparator.run(
            lTask, agentStopwords, agentNothing, 2, learnMinutes*60, 15)
        
        stopwordsFile.write(name+'\t'+str(nFeatures)+'\t'+str(100*confusionStopwords.getAccuracy())+'\n')
        regularFile.write(name+'\t'+str(nFeatures)+'\t'+str(100*confusionNothing.getAccuracy())+'\n')
        
        sig = mcnemar.getSignificance()*100
        matrix = mcnemar.matrix
        
        stopwordsVsRegularFile.write(name+'\t')
        stopwordsVsRegularFile.write(str(matrix[True][True])+'\t'+str(matrix[True][False])+'\t')
        stopwordsVsRegularFile.write(str(matrix[False][True])+'\t'+str(matrix[False][False])+'\t')
        stopwordsVsRegularFile.write(str(sig)+'\t')
        
        if matrix[False][True] < matrix[True][False]:
            stopwordsVsRegularFile.write(str(mcnemar.agent1_name)+'\t'+str(mcnemar.agent2_name)+'\n')
        elif matrix[False][True] > matrix[True][False]:
            stopwordsVsRegularFile.write(str(mcnemar.agent2_name)+'\t'+str(mcnemar.agent1_name)+'\n')
        else:
            stopwordsVsRegularFile.write('None\tNone\n')

        stopwordsFile.close()
        regularFile.close()
        stopwordsVsRegularFile.close()
        totalTime = clock()-startTime
        print 'Task:' , name , ", features:" , nFeatures , ", time =" , totalTime